import torch
from flask import Flask, request
import model_internvl as model
import transformers

device = "cuda"  # the device to load the model onto
print('cuda.is_available()：' + str(torch.cuda.is_available()))
print('cuda.device_count()：' + str(torch.cuda.device_count()))
print('__version__：' + str(torch.__version__))
print('transformers：' + str(transformers.__version__))

app = Flask(__name__)


@app.route('/', methods=['GET'])
def index():
    return '易拍即识'


@app.route('/', methods=['POST'])
def handler():
    print(1)
    data = request.get_json()
    print(data)
    if data is not None:
        image_url = data.get('image_url')
        if not image_url:
            return "参数：image_url不能为空"
        target_text = data.get('target_text')
        if not target_text:
            return "参数：target_text不能为空"

        result = model.recognize(image_url, target_text)
        print(result)
        return result
    else:
        return "请构造对应参数"


if __name__ == '__main__':
    pass  # 移除 app.run() 调用
